/*
 * @Company  iSoftStone Holdings Limited.
 * @Date  Sep 3, 2012
 * @author heweizhi
 */
package com.iss.umservice.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.iss.iframework.common.cfg.SystemPropertyRead;
import com.iss.iframework.security.OperatorUserEntry;
import com.iss.iframework.web.ResultCall;
import com.iss.iframework.web.support.impl.DefaultWebAwareListener;
import com.iss.iframework.web.support.tld.pagination.Navigation;
import com.iss.iframework.web.support.tld.pagination.PageUtil;
import com.iss.iframework.web.support.tld.pagination.Pagination;
import com.iss.umservice.bean.AnnouncementNoReadQueryBean;
import com.iss.umservice.bean.CompanyApplyBean;
import com.iss.umservice.bean.ServiceApplyBean;
import com.iss.umservice.manager.AccountManager;
import com.iss.umservice.manager.AnnouncementManager;
import com.iss.umservice.manager.CompanyManager;
import com.iss.umservice.manager.ServiceManager;
import com.iss.umservice.soa.ValidMenuResponse;

/**
 * 
 * 
 * @author heweizhi
 * @version [1.0, Sep 3, 2012]
 * @since [IBTS_STOCK/1.0]
 * @Company iSoftStone Holdings Limited.
 */
@Controller
@RequestMapping("/index/*")
@SessionAttributes("curUser")
public class LoadMenuController extends DefaultWebAwareListener {
	@Autowired
	private ServiceManager serviceManager;

	@Autowired
	private AccountManager accountManager;

	@Autowired
	private AnnouncementManager announcementManager;

	@Autowired
	private CompanyManager companyManager;

	public String loadWelcome(
			@ModelAttribute("curUser") OperatorUserEntry userEntry,
			Map<String, Object> model) {

		if (!"2".equals(accountManager.queryUserType(userEntry.userIdentifier()))) {
			String[] menuIds = accountManager.queryMenuIds(
					userEntry.userIdentifier(),
					SystemPropertyRead.getValue("system.systemId"));
			if (menuIds == null) {
				menuIds = new String[1];
			}
			List<String> menuIdList = Arrays.asList(menuIds);

			ServiceApplyBean serviceApplyBean = new ServiceApplyBean();
			serviceApplyBean.setStatus("1");
			if (menuIdList.contains("8")) {
				serviceApplyBean.setType("1");
				// 公共服务待审批数
				int serviceToApply_1 = serviceManager
						.countServiceApplyInfo(serviceApplyBean);
				model.put("serviceToApply_1", serviceToApply_1);
			}

			if (menuIdList.contains("9")) {
				serviceApplyBean.setType("2");

				// 共享资源待审批数
				int serviceToApply_2 = serviceManager
						.countServiceApplyInfo(serviceApplyBean);
				model.put("serviceToApply_2", serviceToApply_2);
			}
			if (menuIdList.contains("10")) {
				serviceApplyBean.setType("3");

				// 创业导师申请待审批数
				int tutorToApply = serviceManager
						.countServiceApplyInfo(serviceApplyBean);
				model.put("tutorToApply", tutorToApply);
			}

			// 如果该用户有帐限审批企业信息，则显示等审批个数
			if (menuIdList.contains("102")) {
				CompanyApplyBean compApply = new CompanyApplyBean();
				if (compApply == null) {
					compApply = new CompanyApplyBean();
					compApply.setApplystatus(2);
				} else if (compApply.getApplystatus() == null) {
					compApply.setApplystatus(2);
				}

				int companyToApply = companyManager
						.countCompanyApply(compApply);

				model.put("companyToApply", companyToApply);
			}

			return "welcome";
		} else {

			String currUserId = userEntry.extendsInfo().get("user.accountCode")
					.get(0);

			int count = announcementManager.countNoReadAnn(currUserId);
			String pages = "";
			Pagination pagination = PageUtil.createPage(pages, count,
					"../announcement/queryNoReadAnnouncementList.html");

			List<AnnouncementNoReadQueryBean> annList = announcementManager
					.queryNoReadAnn(currUserId, pagination.getOffset(),
							pagination.getRowSize());

			model.put("annList", annList);
			model.put(Navigation.PAGINATION, pagination);

			return "announcement/lookAnnouncement";
		}
	}

}
